// pages/meetingDetail/meetingDetail.js
var util = require('./../../utils/utils');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    panelFlag: null,
    recordId: null,
    reasonDeail: null,
    recordDetail: null,
  },

  /* 原因输入 */
  getReason: function (e) {
    console.log(e)
    this.setData({
      reasonDeail: e.detail.value
    })
  },



  personDeal: function (option) {

    let dealFlag = option.currentTarget.dataset.flag;
    //对原因是否为空进行检查
    if (!this.data.reasonDeail) {
      wx.showToast({
        title: '原因不能为空',
        icon: 'none'
      })
      return false;
    } else { //不为空更改appointment记录
      var that = this;

      let createTime = util.formatTime(new Date()).replace(/-/g, "/").replace(" ", "-");

      wx.cloud.database().collection('appointment').doc(this.data.recordId).update({
        data: {
          'reason': this.data.reasonDeail,
          'dealTime': createTime,
          'dealPerson': app.globalData.userInfo._openid
        },

        success: res => {

          if (dealFlag === "1") { //同意
            //对记录的数据库再更新一次
            wx.cloud.database().collection('appointment').doc(this.data.recordId).update({
              data: {
                "isDone": 2
              }
            })
            //对的数据库再更新
            wx.cloud.database().collection("meetingRoom").doc(that.data.recordDetail.meetingInfoId).get({
              success: res => {
                let data = res.data;
                let timeList = data.timeList;


                for (var i = 0; i < timeList.length; i++) {
                  console.log(timeList[i].specDate, that.data.recordDetail.checkMyDate);
                  if (timeList[i].specDate === that.data.recordDetail.checkMyDate) {
                    var day = i;
                  }
                }
                console.log(day);

                wx.cloud.database().collection('meetingRoom').doc(this.data.recordDetail.meetingInfoId).update({
                  data: {
                    [`timeList.${day}.openTime.${that.data.recordDetail.checkDay}.state`]: 3,
                  },
                  success: res => {

                    wx.showToast({
                      title: '申请处理成功！！',
                      duration: 1000,
                    })
                    setTimeout(function () {
                      wx.navigateBack({
                        delta: 1
                      })
                    }, 1000)

                  }
                })

              }
            })

          } else { //拒绝

            //对记录的数据库再更新一次
            wx.cloud.database().collection('appointment').doc(this.data.recordId).update({
              data: {
                "isDone": 3
              }
            })
            //对的数据库再更新
            wx.cloud.database().collection("meetingRoom").doc(that.data.recordDetail.meetingInfoId).get({
              success: res => {
                let data = res.data;
                let timeList = data.timeList;


                for (var i = 0; i < timeList.length; i++) {
                  console.log(timeList[i].specDate, that.data.recordDetail.checkMyDate);
                  if (timeList[i].specDate === that.data.recordDetail.checkMyDate) {
                    var day = i;
                  }
                }
                console.log(day);

                wx.cloud.database().collection('meetingRoom').doc(this.data.recordDetail.meetingInfoId).update({
                  data: {
                    [`timeList.${day}.openTime.${that.data.recordDetail.checkDay}.state`]: 1,
                    [`timeList.${day}.openTime.${that.data.recordDetail.checkDay}.appointmentId`]: null,
                  },
                  success: res => {
                    wx.showToast({
                      title: '申请处理成功！！',
                      duration: 1000,
                    })
                    setTimeout(function () {
                      wx.navigateBack({
                        delta: 1
                      })
                    }, 1000)
                  }
                })

              }
            })
          }

        }
      })

    }

  },

  /* 同意 */
  agree: function () {
    //对原因是否为空进行检查
    if (!this.data.reasonDeail) {
      wx.showToast({
        title: '原因不能为空',
        icon: 'none'
      })
      return false;
    } else { //不为空更改appointment记录
      let createTime = util.formatTime(new Date()).replace(/-/g, "/").replace(" ", "-");

      wx.cloud.database().collection('appointment').doc(this.data.recordId).update({
        data: {
          'isDone': 1,
          'reason': this.data.reasonDeail,
          'dealTime': createTime,
          'dealPerson': app.globalData.userInfo._openid
        },
        success: res => {
          wx.showToast({
            title: '申请处理成功！！',
            duration: 1000,
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/allRecords/allRecords'
            })
          }, 1000)
        }
      })


    }
  },

  /* 驳回 */
  disagree: function () {
    //对原因是否为空进行检查
    if (!this.data.reasonDeail) {
      wx.showToast({
        title: '原因不能为空',
        icon: 'none'
      })
      return false;
    } else { //不为空更改appointment记录
      let createTime = util.formatTime(new Date()).replace(/-/g, "/").replace(" ", "-");

      wx.cloud.database().collection('appointment').doc(this.data.recordId).update({
        data: {
          'resultFlag': 2,
          'reason': this.data.reasonDeail,
          'dealTime': createTime,
          'dealPerson': app.globalData.userInfo._openid
        },
        success: res => {
          wx.showToast({
            title: '申请处理成功！！',
            duration: 1000,
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/allRecords/allRecords'
            })
          }, 1000)
        }
      })


    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '载入数据中',
    })

    this.setData({
      recordId: options.record,
      panelFlag: Number(options.flag)
    })


    //发起数据请求
    wx.cloud.database().collection("appointment").doc(this.data.recordId).get({
      success: res => {
        wx.hideLoading()
        this.setData({
          recordDetail: res.data
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})